package com.ruoyi.system.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.Livebroadcastingroom;
import com.ruoyi.system.domain.SignHistory;
import com.ruoyi.system.domain.StudioAttention;
import com.ruoyi.system.domain.StudioRecommend;
import com.ruoyi.system.service.ILivebroadcastingroomService;
import com.ruoyi.system.service.ISignHistoryService;
import com.ruoyi.system.service.IStudioAttentionService;
import com.ruoyi.system.service.IStudioRecommendService;
import com.ruoyi.system.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 直播模块Controller
 *
 * @author sjt
 * @date 2021-12-1
 */
@RestController
@RequestMapping("/LiveModular")
public class LiveModularController extends BaseController {
    @Autowired
    private LiveModularServiceImpl liveModularService;

    @Autowired
    private ILivebroadcastingroomService livebroadcastingroomService;

    @Autowired
    private IStudioRecommendService studioRecommendService;

    @Autowired
    private IStudioAttentionService studioAttentionService;

    @Autowired
    private ISignHistoryService iSignHistoryService;

    /**
     * 获取直播间列表(TBC：是否开播)
     */
    @GetMapping("/getLiveRoom")
    public TableDataInfo getLiveRoom(Livebroadcastingroom livebroadcastingroom)
    {
        startPage();
        List<Livebroadcastingroom> list = livebroadcastingroomService.selectLivebroadcastingroomList(livebroadcastingroom);
        return getDataTable(list);
    }

    /**
     * 获取推荐直播间列表(TBC：是否开播)
     */
    @GetMapping("/recommend")
    public TableDataInfo recommend(StudioRecommend studioRecommend)
    {
        List<StudioRecommend> list = studioRecommendService.selectStudioRecommendList(studioRecommend);
        return getDataTable(list);
    }

    /**
     * 关注主播
     * */
    @PostMapping("/attention")
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public AjaxResult attention(@RequestBody StudioAttention studioAttention){
        //查询数据库 判断是否已关注
        List<StudioAttention> list = studioAttentionService.selectStudioAttentionList(studioAttention);
        if(list.size() == 0){
            int key = studioAttentionService.insertStudioAttention(studioAttention);
            if(key == 1){
                return new AjaxResult(200,"关注成功");
            }
        }else{
            if(studioAttentionService.deleteStudioAttentionById(list.get(0).getId()) == 1){
                return new AjaxResult(200,"取消关注成功");
            }
        }
        return new AjaxResult(500,"关注操作时发生，未知错误。");
    }

    /**
     * 获取直播间信息
     */
    @GetMapping("/anchorInfo")
    public AjaxResult anchorInfo(Long roomId)
    {
        if(roomId==null){
            return AjaxResult.error("roomId为空");
        }
        return AjaxResult.success(liveModularService.selectLivebroadcastingroomByRoomId(roomId));
    }

    /**
     * 开通直播间
     */
    @PostMapping("/updatauserinfo")
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public AjaxResult updatauserinfo(@RequestBody Livebroadcastingroom livebroadcastingroom)
    {
        if(livebroadcastingroom.getUserId()==null){
            return AjaxResult.error("未传入用户编号");
        }else if(livebroadcastingroom.getAppid()==null){
            return AjaxResult.error("未传入授权码");
        }
        return toAjax(livebroadcastingroomService.insertLivebroadcastingroom(livebroadcastingroom));
    }

    /**
     * 获取用户签到历史
     * */
    @GetMapping("/getSignHistory")
    public AjaxResult getSignHistory(Long userId){
        SignHistory signHistory = iSignHistoryService.selectSignHistoryByUserId(userId);
        return AjaxResult.success(signHistory);
    }

    /**
     * 修改直播间信息
     */
    @PostMapping("/updataroom")
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public AjaxResult edit(@RequestBody Livebroadcastingroom livebroadcastingroom)
    {
        return toAjax(livebroadcastingroomService.updateLivebroadcastingroom(livebroadcastingroom));
    }

    /**
     * 返回UserSig
     */
    @GetMapping("/getUserSig")
    private AjaxResult getUserSig(String userId)
    {
        return AjaxResult.success("userSig",liveModularService.generateUserSig(userId));
    }
}
